

require "TSLib"
init(0)
main_tab = {
    "1f0ffa00401800ff87c0000003e1ff400803001ff@0$00$59$11$15",
    }
main_index = addTSOcrDictEx(main_tab)


-- grande_tab = {
--     "1800030000f0003fffffffffffffdff9c@0$1$77$19$7",
--     "018103ff1c7ff9efff9ff0387e0307fffffffffe7fffc3fffc$9$140$20$10",
--     "001f03f7fc7fffefffffe1c07e0c07fffff7fffe7f7fe3e3fc$8$143$20$10",
--     "7c0fc7c0fefc0fefccf7e0c07e1e0ffffff7fffe3f3fc$3$122$20$9",
--     "1000e7c03df81fff0ffe07ffc3fdfffe3bff073fc0@111$2$107$19$9",
--     "a79b1ff3f3fe7f7fcfec601f9c01f3fff67ffec7ff@100$5$119$19$9",
--     "c00030003c01ff0fffffffffff8ffc0@00$7$72$18$7",
--     "3fffc7fffe7fffefffffe0c07e1c07f9ffff9ffe39ffe$6$139$20$9",
--     "008803fffc7fffeffffffffffe0007fdcff7fffe7fffe1fff8$0$149$20$10",
--     "003e007f81ffe1fe38f80e3fffffffffffffffff@11$4$116$18$9",
--     }
-- grande_index = addTSOcrDictEx(grande_tab)
-- grande_ret = tsOcrText(grande_index,  408, 27, 457, 51, "FED184 , 022462", 90)
    


-- build_spacing = 150;  
build_spacing = 75;-- x y  通用
build_color = 0x48ae51;
exec_count = 0;  -- 执行计数
graden = 0; -- 分数


tap_left_x = 35;
tap_left_y = 753;
tap_right_x = 480;
tap_right_y = 792;


obstacle_color = 0x579b3b; -- 障碍物
obstacle_spacing_y = 50;  -- 障碍物间距
obstacle_spacing_x = 10;

w,h = getScreenSize();
x= -1;
y= -1;
original_color = 0x48ae51; -- 源点
enable_exit = false




-- left --right
function search_path(x,y,last_path) 
    local lx = x-build_spacing;
    local ly = y-build_spacing;
    local rx = x+build_spacing;
    local ry = y-build_spacing;


    if x < 150 and y < 150 then
        return -1;
    end

    if isColor(lx,ly,build_color) == true and (isColor(lx+obstacle_spacing_x, ly-obstacle_spacing_y, obstacle_color,85) == false) then
        

        return 0;
    end

    if isColor(rx,ry,build_color) == true and (isColor(rx-obstacle_spacing_x, ry-obstacle_spacing_y, obstacle_color,85) == false) then
        return 1;
    end

        return -1;

    
end


function loop_exec_script() 


    exec_path = {}; -- 路径 0 left , 1 right, -1 无路可走
    x =   271;  
    y =  576;

    while isColor(x,y,original_color) == false do
    end


    while true do
        exec_res = -1;
        exec_res = search_path(x,y,nil);
        if exec_res == 1 then
            x = x + build_spacing;
            y = y - build_spacing;
            
        elseif exec_res == 0 then
            x = x - build_spacing;
            y = y - build_spacing;
        
        else
            if graden >= 5000 then
                enable_exit = true
            end 
            break
        end;
        table.insert(exec_path, exec_res);
        exec_count=exec_count+1
    end


    for i, v in ipairs(exec_path) do
        if v == 0 then
            tap(tap_left_x,tap_left_y);
        elseif v == 1 then
            tap(tap_right_x,tap_right_y);
        end
    end 
    x= 540;
    y=1150;


end


main_ret = tsOcrText(main_index, 150, 34, 170, 50, "FFFFFF , 010101", 90)
while '00' ~= main_ret do
    main_ret = tsOcrText(main_index, 150, 34, 170, 50, "FFFFFF , 010101", 90)
end


main_ret = tsOcrText(main_index, 150, 34, 170, 50, "FFFFFF , 010101", 90)
while '00' == main_ret do
    main_ret = tsOcrText(main_index, 150, 34, 170, 50, "FFFFFF , 010101", 90)
end


while true do
    if enable_exit then 
        break
    end
    loop_exec_script()
    mSleep(400)
end 
